<template>
	<div class="dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" :close-on-click-modal="false" width="500px">
			<el-form ref="formRef" :model="state.ruleForm" :rules="state.rules">
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="当前虚拟销量" prop="virtual_sales">
							<el-input v-model="state.ruleForm.virtual_sales" disabled />
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="销量操作" prop="type">
							<el-radio-group v-model="state.ruleForm.type">
								<el-radio :value="1">加销量</el-radio>
								<el-radio :value="2">减销量</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>
				</el-row>
				<el-row class="mb20" :gutter="20">
					<el-col :span="24">
						<el-form-item label="数量" prop="number">
							<el-input v-model="state.ruleForm.number" @input="onVerifiyNumberIntegerNumber" />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<el-button @click="onCancel" size="default">取 消</el-button>
				<el-button type="primary" @click="onSubmit" :loading="state.dialog.submitLoading" size="default">提 交</el-button>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus';
import { reactive, ref } from 'vue';
import { goodsApi } from '/@/api';
import { verifiyNumberInteger } from '/@/utils/toolsValidate';

let callback: Function = () => {};
const GoodsApi = goodsApi();
const validatePersonNumber = (rule: any, value: string, callback: any) => {
	if (state.ruleForm.type === 1) callback();
	if (Number(value) <= Number(state.ruleForm.virtual_sales)) {
		callback();
	} else {
		callback(new Error('操作减销量时，数量不能大于当前销量'));
	}
};
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	},
	ruleForm: {
		goods_id: '',
		virtual_sales: '',
		number: '',
		type: 1,
	},
	rules: {
		number: [
			{ required: true, message: '数量不能为空', trigger: ['blur'] },
			{ required: true, validator: validatePersonNumber, trigger: ['blur'] },
		],
	},
});
const formRef = ref();

/********** 触发事件 **********/
// 销量过滤
const onVerifiyNumberIntegerNumber = (val: string) => {
	state.ruleForm.number = verifiyNumberInteger(val);
};
// 取消
const onCancel = () => {
	state.dialog = {
		isShowDialog: false,
		type: '',
		title: '',
		submitLoading: false,
	};
};
// 提交
const onSubmit = () => {
	formRef.value.validate((valid: boolean) => {
		if (!valid) return;
		_goodsEditVirtualSales(state.ruleForm);
	});
};

/********** 方法事件 **********/
// 打开
const openDialog = (row: any, fn: Function) => {
	state.dialog.isShowDialog = true;
	state.dialog.title = `商品「${row.title}」销量修改`;
	state.ruleForm = {
		goods_id: row.goods_id,
		virtual_sales: row.virtual_sales,
		number: '',
		type: 1,
	};
	callback = fn;
};

/********** 网络请求 **********/
// 加或减销量
const _goodsEditVirtualSales = (params: Object) => {
	GoodsApi.goodsEditVirtualSales(params)
		.then(() => {
			ElMessage.success('操作成功！');
			onCancel();
			let virtual_sales = 0;
			if (state.ruleForm.type === 1) {
				virtual_sales = Number(state.ruleForm.virtual_sales) + Number(state.ruleForm.number);
			} else {
				virtual_sales = Number(state.ruleForm.virtual_sales) - Number(state.ruleForm.number);
			}
			callback(virtual_sales);
		})
		.catch(() => (state.dialog.submitLoading = false));
};

/********** 方法向父组件暴露 **********/
defineExpose({ openDialog });
</script>
